package com.morehair.controller.admin;

import com.morehair.bean.bo.admin.CouponReq;
import com.morehair.bean.pojo.Coupon;
import com.morehair.bean.pojo.CouponUser;
import com.morehair.bean.vo.admin.BaseData;
import com.morehair.bean.vo.admin.BaseRespVo;
import com.morehair.bean.bo.admin.CouponUserReq;
import com.morehair.service.admin.CouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: DLX
 * @Date: 2021/6/5 21:46
 * @Description:
 **/

@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    @RequiresPermissions("admin:coupon:list")
    public BaseRespVo couponList(CouponReq couponReq) {

        BaseData<Coupon> couponData = couponService.query(couponReq);
        return BaseRespVo.ok(couponData);
    }

    @RequestMapping("read")
    @RequiresPermissions("admin:coupon:read")
    public BaseRespVo couponDetail(Integer id) {

        Coupon coupon = couponService.selectById(id);
        return BaseRespVo.ok(coupon);
    }

    @RequestMapping("listuser")
    @RequiresPermissions("admin:coupon:listuser")
    public BaseRespVo couponListUser(CouponUserReq couponUserReq) {
        BaseData<CouponUser> couponUserData = couponService.queryCouponUserList(couponUserReq);
        return BaseRespVo.ok(couponUserData);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:coupon:create")
    public BaseRespVo couponCreate(@RequestBody Coupon coupon) {

        Coupon newCoupon = couponService.create(coupon);
        if (newCoupon == null) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(newCoupon);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:coupon:update")
    public BaseRespVo couponUpdate(@RequestBody Coupon coupon) {
        Coupon updatedCoupon = couponService.couponUpdate(coupon);
        if (updatedCoupon == null) {
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(updatedCoupon);
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:coupon:delete")
    public BaseRespVo couponDelete(@RequestBody Coupon coupon) {
        int code = couponService.couponDelete(coupon);

        if (code == 200) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

}
